/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package nry;

/**
 *
 * @author Cesar
 */
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;

public class juego implements ActionListener {

	private JFrame window = new JFrame("Tres en Raya UCSP");
	private JMenuBar menu = new JMenuBar();
	private JLabel button1 = new JLabel("TIPO 1");
	private JLabel button2 = new JLabel("TIPO 2");
	//private JLabel button3 = new JLabel("TIPO 3");
	private JLabel label = new JLabel("       Elegir tipo:     ");
	private JCheckBox check = new JCheckBox();
	private JCheckBox check1 = new JCheckBox();
	//private JCheckBox check2 = new JCheckBox();
	public void GUI(){
		window.setSize(300,200);
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setLayout(new GridLayout(4,3));
		menu.add(label);
		menu.add(button1);
		menu.add(check);
		menu.add(button2);
		menu.add(check1);
//		menu.add(button3);
		//menu.add(check2);
		window.add(menu);

		check.addActionListener(this);
		check1.addActionListener(this);
		//check2.addActionListener(this);

		window.setVisible(true);
	}
public static void main(String[] args) {
	// TODO Auto-generated method stub
	new juego().GUI();
}
@Override
public void actionPerformed(ActionEvent a) {
	// TODO Auto-generated method stub
	if(a.getSource() == check){
		System.out.println("presionado tipo 1");
		Tres nuevo = factory.creatablero(0);
		nuevo.TresRaya();
	}
	else if(a.getSource() == check1){
		System.out.println("presionado tipo 2");
		Tres nuevo = factory.creatablero(1);
		nuevo.TresRaya();
	}
	/*else if(a.getSource() == check2){
		System.out.println("presionado tipo 3");
		Tres nuevo = factory.creatablero(2);
		nuevo.TresRaya();
	}*/
}

}
abstract class Tres {
	public abstract void TresRaya();
}

class TresRaya1 extends Tres implements ActionListener {
	/*Inst6ance Variables*/
	private JFrame window = new JFrame("Tic-Tac-Toe");
	private JButton button1 = new JButton("");
	private JButton button2 = new JButton("");
	private JButton button3 = new JButton("");
	private JButton button4 = new JButton("");
	private JButton button5 = new JButton("");
	private JButton button6 = new JButton("");
	private JButton button7 = new JButton("");
	private JButton button8 = new JButton("");
	private JButton button9 = new JButton("");

	private String letter = "";
  	private int count = 0;
	private boolean win = false;

	static boolean instance_flag = false;

	public void TresRaya(){
	/*Create Window*/
	window.setSize(300,200);
	window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	window.setLayout(new GridLayout(3,3));

	/*Add Buttons To The Window*/
	window.add(button1);
	window.add(button2);
	window.add(button3);
	window.add(button4);
	window.add(button5);
	window.add(button6);
	window.add(button7);
	window.add(button8);
	window.add(button9);
    /*Add The Action Listener To The Buttons*/
	button1.addActionListener(this);
	button2.addActionListener(this);
	button3.addActionListener(this);
	button4.addActionListener(this);
	button5.addActionListener(this);
	button6.addActionListener(this);
	button7.addActionListener(this);
	button8.addActionListener(this);
	button9.addActionListener(this);
	/*Make The Window Visible*/
	window.setVisible(true);

	}


	public void actionPerformed(ActionEvent a) {
		count++;

		/*Calculate Who's Turn It Is*/
		if(count % 2 == 0)
		{
			letter = "O";
		}
		else
		{
			letter = "X";
		}


		/*Display X's or O's on the buttons*/
		if(a.getSource() == button1){
			button1.setText(letter);
			button1.setEnabled(false);
		} else if(a.getSource() == button2){
			button2.setText(letter);
			button2.setEnabled(false);
		} else if(a.getSource() == button3){
			button3.setText(letter);
			button3.setEnabled(false);
		} else if(a.getSource() == button4){
			button4.setText(letter);
			button4.setEnabled(false);
		} else if(a.getSource() == button5){
			button5.setText(letter);
			button5.setEnabled(false);
		} else if(a.getSource() == button6){
			button6.setText(letter);
			button6.setEnabled(false);
		} else if(a.getSource() == button7){
			button7.setText(letter);
			button7.setEnabled(false);
		} else if(a.getSource() == button8){
			button8.setText(letter);
			button8.setEnabled(false);
		} else if(a.getSource() == button9){
			button9.setText(letter);
			button9.setEnabled(false);
		}

		/*Determine who won*/
		//horizontal wins
		if( button1.getText() == button2.getText() && button2.getText() == button3.getText() && button1.getText() != ""){
			win = true;
		}
		else if(button4.getText() == button5.getText() && button5.getText() == button6.getText() && button4.getText() != ""){
			win = true;
		}
		else if(button7.getText() == button8.getText() && button8.getText() == button9.getText() && button7.getText() != ""){
			win = true;
		}

		//virticle wins
		else if(button1.getText() == button4.getText() && button4.getText() == button7.getText() && button1.getText() != ""){
			win = true;
		}
		else if(button2.getText() == button5.getText() && button5.getText() == button8.getText() && button2.getText() != ""){
			win = true;
		}
		else if(button3.getText() == button6.getText() && button6.getText() == button9.getText() && button3.getText() != ""){
			win = true;
		}

		//diagonal wins
		else if(button1.getText() == button5.getText() && button5.getText() == button9.getText() && button1.getText() != ""){
			win = true;
		}
		else if(button3.getText() == button5.getText() && button5.getText() == button7.getText() && button3.getText() != ""){
			win = true;
		}
		else {
			win = false;
		}

		/*Show a dialog if someone wins or the game is tie*/
		if(win == true){
			JOptionPane.showMessageDialog(null, letter + " WINS!");
		} else if(count == 9 && win == false){
			JOptionPane.showMessageDialog(null, "Tie Game!");
		}
	}
}

class TresRaya extends Tres implements ActionListener {
	/*Instance Variables*/
	private JFrame window = new JFrame("Tic-Tac-Toe");
	private JButton button1 = new JButton("");
	private JButton button2 = new JButton("");
	private JButton button3 = new JButton("");
	private JButton button4 = new JButton("");
	private JButton button5 = new JButton("");
	private JButton button6 = new JButton("");
	private JButton button7 = new JButton("");
	private JButton button8 = new JButton("");
	private JButton button9 = new JButton("");

	private String letter = "";
  	private int count = 0;
	private boolean win = false;

	static boolean instance_flag = false;

	public void TresRaya(){
	/*Create Window*/
	window.setSize(300,300);
	window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	window.setLayout(new GridLayout(3,3));

	/*Add Buttons To The Window*/
	window.add(button1);
	window.add(button2);
	window.add(button3);
	window.add(button4);
	window.add(button5);
	window.add(button6);
	window.add(button7);
	window.add(button8);
	window.add(button9);
    /*Add The Action Listener To The Buttons*/
	button1.addActionListener(this);
	button2.addActionListener(this);
	button3.addActionListener(this);
	button4.addActionListener(this);
	button5.addActionListener(this);
	button6.addActionListener(this);
	button7.addActionListener(this);
	button8.addActionListener(this);
	button9.addActionListener(this);
	/*Make The Window Visible*/
	window.setVisible(true);

	}

	public void actionPerformed(ActionEvent a) {
		count++;

		/*Calculate Who's Turn It Is*/
		if(count % 2 == 0)
		{
			letter = "O";
		}
		else
		{
			letter = "X";
		}


		/*Display X's or O's on the buttons*/
		if(a.getSource() == button1){
			button1.setText(letter);
			button1.setEnabled(false);
		} else if(a.getSource() == button2){
			button2.setText(letter);
			button2.setEnabled(false);
		} else if(a.getSource() == button3){
			button3.setText(letter);
			button3.setEnabled(false);
		} else if(a.getSource() == button4){
			button4.setText(letter);
			button4.setEnabled(false);
		} else if(a.getSource() == button5){
			button5.setText(letter);
			button5.setEnabled(false);
		} else if(a.getSource() == button6){
			button6.setText(letter);
			button6.setEnabled(false);
		} else if(a.getSource() == button7){
			button7.setText(letter);
			button7.setEnabled(false);
		} else if(a.getSource() == button8){
			button8.setText(letter);
			button8.setEnabled(false);
		} else if(a.getSource() == button9){
			button9.setText(letter);
			button9.setEnabled(false);
		}

		/*Determine who won*/
		//horizontal wins
		if( button1.getText() == button2.getText() && button2.getText() == button3.getText() && button1.getText() != ""){
			win = true;
		}
		else if(button4.getText() == button5.getText() && button5.getText() == button6.getText() && button4.getText() != ""){
			win = true;
		}
		else if(button7.getText() == button8.getText() && button8.getText() == button9.getText() && button7.getText() != ""){
			win = true;
		}

		//virticle wins
		else if(button1.getText() == button4.getText() && button4.getText() == button7.getText() && button1.getText() != ""){
			win = true;
		}
		else if(button2.getText() == button5.getText() && button5.getText() == button8.getText() && button2.getText() != ""){
			win = true;
		}
		else if(button3.getText() == button6.getText() && button6.getText() == button9.getText() && button3.getText() != ""){
			win = true;
		}

		//diagonal wins
		else if(button1.getText() == button5.getText() && button5.getText() == button9.getText() && button1.getText() != ""){
			win = true;
		}
		/*else if(button3.getText() == button5.getText() && button5.getText() == button7.getText() && button3.getText() != ""){
			win = true;
		}*/
		else {
			win = false;
		}

		/*Show a dialog if someone wins or the game is tie*/
		if(win == true){
			JOptionPane.showMessageDialog(null, letter + " WINS!");
		} else if(count == 9 && win == false){
			JOptionPane.showMessageDialog(null, "Tie Game!");
		}
	}
}

class factory{//se encarga de crear
	public static final int tipo = 0, tipo1 = 1, tipo2 = 3;
	public factory(){};
	public static Tres creatablero(int a){
		 if (a == factory.tipo){
	            return new TresRaya();
	        }
	     if (a==factory.tipo1){
	            return new TresRaya1();
	        }
	     if (a==factory.tipo2){
	            return new TresRaya1();
	     }
        return null;
	}
}



